package com.jingtai.platform.service.merchants.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingtai.common.enums.DeleteFlagEnum;
import com.jingtai.common.enums.OfflineEnum;
import com.jingtai.common.template.ResultMapInfo;
import com.jingtai.core.entity.merchants.UserStore;
import com.jingtai.core.mapper.merchants.UserStoreMapper;
import com.jingtai.platform.service.merchants.UserStoreService;
import com.jingtai.platform.util.ResultUtil;
import com.jingtai.platform.util.UrlToMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * 商户店铺表 impl
 *
 * @author sunlaiqian
 * @date 2020-12-02 18:31:32
 */
@Service
public class UserStoreServiceImpl extends ServiceImpl<UserStoreMapper, UserStore> implements UserStoreService {

    @Autowired
    private UserStoreService userStoreService;

    @Override
    public ResultUtil UserStorelist(Integer page, Integer limit, String conditions){
        QueryWrapper<UserStore> wrapper = new QueryWrapper<>();
        Map<String, String> map = UrlToMap.paramToMap(conditions);
        for(Map.Entry<String, String> entry : map.entrySet()){
            wrapper.like(entry.getKey(), entry.getValue());
        }
        wrapper.eq("delete_flag", DeleteFlagEnum.NOT_DELETE.getCode());
        return new ResultUtil(ResultMapInfo.GETSUCCESS,userStoreService.page(new Page<>(page,limit),wrapper));
    }

    @Override
    public ResultUtil addUserStore(UserStore userStore) {
        boolean save = userStoreService.save(userStore);
        return new ResultUtil(save ? ResultMapInfo.ADDSUCCESS:ResultMapInfo.ADDFAIL);
    }

    @Override
    public ResultUtil getUserStoreById(Long id) {
        UserStore userStore = userStoreService.getById(id);
        return userStore == null ?  new ResultUtil(ResultMapInfo.GETFAIL) : new ResultUtil(ResultMapInfo.GETSUCCESS,userStore);
    }

    @Override
    public ResultUtil delUserStoreById(Long id) {
        UserStore userStore = new UserStore();
        userStore.setId(id);
        userStore.setDeleteFlag(DeleteFlagEnum.DELETE.getCode());
        boolean delState = userStoreService.updateById(userStore);
        return delState ?  new ResultUtil(ResultMapInfo.DELETESUCCESS) : new ResultUtil(ResultMapInfo.DELETEFAIL);
    }

    @Override
    public ResultUtil getUserStoreByUserType(Long userType) {
        List<UserStore> list = userStoreService.list(Wrappers.lambdaQuery(UserStore.class).eq(UserStore::getUserType, userType).eq(UserStore::getDeleteFlag,DeleteFlagEnum.NOT_DELETE.getCode()).eq(UserStore::getIsOffline, OfflineEnum.OFFLINE_YES.getValue()));
        return new ResultUtil(ResultMapInfo.GETSUCCESS,list);
    }

    @Override
    public ResultUtil editUserStore(UserStore userStore) {
        boolean editState = userStoreService.updateById(userStore);
        return editState ?  new ResultUtil(ResultMapInfo.EDITSUCCESS) : new ResultUtil(ResultMapInfo.EDITFAIL);
    }

}
